import { Base64 } from 'js-base64';

export function groupArray(array, groupSize) {
    return array.reduce((result, item, index) => {
        const chunkIndex = Math.floor(index / groupSize); // 计算当前元素所在的组索引
        if (!result[chunkIndex]) {
            result[chunkIndex] = []; // 新建一个组
        }
        result[chunkIndex].push(item); // 将元素添加到当前组
        return result;
    }, []);
}

const complement = function (value) {
    return value < 10 ? `0${value}` : value;
};

export const formateDate = (date) => {
    const time = new Date(date);
    const year = time.getFullYear();
    const month = complement(time.getMonth() + 1);
    const day = complement(time.getDate());
    const hour = complement(time.getHours());
    const minute = complement(time.getMinutes());
    const second = complement(time.getSeconds());
    const week = '日一二三四五六'.charAt(time.getDay());

    return {
        week,
        year: `${year}年`,
        month,
        day,
        date: `${year}-${month}-${day}`,
        time: `${hour}:${minute}:${second}`
    }
};

// 根据环境拼接图片
export function jointFilePath(path?:string):string {
    let { MODE, VITE_APP_BASE_URL } = import.meta.env
    let baseUrl:string = MODE == 'development'? VITE_APP_BASE_URL : location.origin + '/'
    return baseUrl + path
}

export function previewFile(path:string) {
    const pathUrl = jointFilePath(path)
    return `http://kkfile.sugonup.com/onlinePreview?url=${Base64.encode(pathUrl)}`
}

export const openUrl = (url:string) => {
    window.open(url)
}